import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import {REQ_URL} from '../Constants'
import { HttpService } from '../httpService'
/*
  Generated class for the DeviceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DeviceProvider {

  constructor(public http: Http,
              public httpService : HttpService) {
    //console.log('Hello DeviceProvider Provider');
  }

  /**
   * 获取gis列表
   * @param obj
   */
  getGisList(obj?:any){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.gis.getList ,obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }
  /**
   * 获取gis列表
   * @param obj
   */
  getStateCount(obj?:any){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.gis.getStateCount ,obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备列表信息
   * @param obj
   */
  getList(obj?:any){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.dev.getList ,obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备详细信息
   * @param obj
   */
  getDetail(id){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.dev.getDetail+ "/"+ id)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 更新设备的一些状态。时间段
   * @param obj
   */
  updateStatus(obj){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.dev.updateStatus + '/' + obj.id, obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

    /**
     * 添加设备
     * @param obj
     */
    addDevice(obj){
        return new Promise((resolve, reject) => {
            console.log(obj);
            this.httpService.postBody( REQ_URL.dev.platformAddDevice, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

    /**
     * 修改设备
     * @param obj
     */
    updateDevice(obj){
        return new Promise((resolve, reject) => {
            this.httpService.postBody( REQ_URL.dev.platformUpdateDevice, obj)
                .then(data => {
                    resolve(data);
                }, err => {
                    reject(err);
                });
        });
    }

  /**
   * 更新设备GPS，下发指令
   * @param obj
   */
  updateGPS(id){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.dev.updateGPS + '/' + id)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备报警列表信息
   * @param obj
   */
  getAlarmList(obj?:any){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.alarm.getList ,obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备报警详细信息
   * @param obj
   */
  getAlarmDetail(physicalCode){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.alarm.getDetail+ "/"+ physicalCode)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备故障列表信息
   * @param obj
   */
  getBadList(obj?:any){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.alarm.getList ,obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 获取设备故障详细信息
   * @param obj
   */
  getBadDetail(id){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.alarm.getDetail+ "/"+ id)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

  /**
   * 更新故障细节
   * @param obj
   */
  updateBadDetail(obj){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.alarm.update, obj)
        .then(data => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }


  /**
   * 获取地市列表
   * @param obj
   */
  getCityList(proCode){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.area.getCityList+ "/"+ proCode)
          .then(data => {
            resolve(data);
          }, err => {
            reject(err);
          });
    });
  }

  /**
   * 获取县列表区
   * @param obj
   */
  getDistrictList(cityCode){
    return new Promise((resolve, reject) => {
      this.httpService.postBody( REQ_URL.area.getDistrictList+ "/"+ cityCode)
          .then(data => {
            resolve(data);
          }, err => {
            reject(err);
          });
    });
  }

  /**
   *获取农场列表
   */
  getFarmList(){
      return new Promise((resolve, reject) => {
          this.httpService.postBody( REQ_URL.farm.getAllList)
              .then(data => {
                  resolve(data);
              }, err => {
                  reject(err);
              });
      });
  }


}
